package demo_0421.homework;

public class Race {
    private static final int TOTAL_DISTANCE = 20;
    private static  boolean hasFinish = false;

    public static void main(String[] args) {
        System.out.println("比赛开始，总长度为 " + TOTAL_DISTANCE + "米");
        Thread turtle = new Thread(new Turtle());
        turtle.setName("乌龟");
        Thread rabbit = new Thread(new Rabbit());
        rabbit.setName("兔子");

        turtle.start();
        rabbit.start();
        try {
            turtle.join();
            rabbit.join();
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println("比赛结束");
    }


    static class Turtle extends Thread{
        private static final double SPEED = 0.1;
        @Override
        public void run() {
            double distance = 0.0;
            while (distance < TOTAL_DISTANCE && !hasFinish) {
                try {
                    Thread.sleep(1000);
                    distance += SPEED;
                    System.out.printf("%s 已经跑了 %.2f 米\n",Thread.currentThread().getName(),distance);
                    
                    if (distance >= TOTAL_DISTANCE && !hasFinish) {
                        synchronized (Race.class) {
                            if (!hasFinish) {
                                hasFinish = true;
                                System.out.printf("%s 首个到达终点,比赛结束\n",Thread.currentThread().getName());
                                System.exit(0);
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }


    static class Rabbit implements Runnable{
        //速度 0.5米每秒
        private static final double SPEED = 0.5;
        //休息间隔  2.0m
        private static final double GAP = 2.0;
        //休息时长 10s
        private static final long REST_TIME = 10000;

        @Override
        public void run() {
            double distance = 0.0;
            double restdistance = 0.0;
            while (distance < TOTAL_DISTANCE && !hasFinish) {
                try {
                    Thread.sleep(1000);
                    distance += SPEED;
                    restdistance += SPEED;
                    System.out.printf("%s 已经跑了 %.2f 米\n",Thread.currentThread().getName(),distance);

                    if (restdistance >= GAP && distance < TOTAL_DISTANCE) {
                        restdistance = 0.0;
                        System.out.println(Thread.currentThread().getName() + " 开始休息 " + REST_TIME / 1000 + " 秒");
                        Thread.sleep(REST_TIME);
                        System.out.printf("%s 休息结束\n",Thread.currentThread().getName());
                    }
        
                    if (distance >= TOTAL_DISTANCE && !hasFinish) {
                        synchronized (Race.class) {
                            if (!hasFinish) {
                                hasFinish = true;
                                System.out.printf("%s 首个到达终点,比赛结束\n",Thread.currentThread().getName());
                                System.exit(0);
                            }
                        }
                    }
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
